package com.ruoyi.house_sale.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.house_sale.domain.ResearchExam;
import com.ruoyi.house_sale.service.IResearchExamService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house_sale.domain.ExamMaster;
import com.ruoyi.house_sale.service.IExamMasterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 答卷题目Controller
 * 
 * @author ruoyi
 * @date 2022-03-28
 */
@RestController
@RequestMapping("/house_sale/ExamMaster")
public class ExamMasterController extends BaseController
{
    @Autowired
    private IExamMasterService examMasterService;
    @Resource
    private IResearchExamService researchExamService;

    /**
     * 查询答卷题目列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:list')")
    @GetMapping("/list/{reseachid}")
    public TableDataInfo list(@PathVariable("reseachid") Long reseachid)
    {
        List<ExamMaster> list = examMasterService.selectExamMasterList(reseachid);
        return getDataTable(list);
    }

    /**
     * 导出答卷题目列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:export')")
    @Log(title = "答卷题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamMaster examMaster)
    {
        List<ExamMaster> list = examMasterService.selectExamMaster(examMaster);
        ExcelUtil<ExamMaster> util = new ExcelUtil<ExamMaster>(ExamMaster.class);
        util.exportExcel(response, list, "答卷题目数据");
    }

    /**
     * 获取答卷题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:query')")
    @GetMapping(value = "/{examid}")
    public AjaxResult getInfo(@PathVariable("examid") Long examid)
    {
        return AjaxResult.success(examMasterService.selectExamMasterByExamid(examid));
    }

    /**
     * 新增答卷题目
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:add')")
    @Log(title = "答卷题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamMaster examMaster)
    {
        int examcount = examMasterService.selCountId(examMaster.getReseachid());
        int count = researchExamService.updExamCount(examcount+1,examMaster.getReseachid());
        System.out.println("count="+count);
        return toAjax(examMasterService.insertExamMaster(examMaster));
    }


    /**
     * 修改答卷题目
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:edit')")
    @Log(title = "答卷题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamMaster examMaster)
    {
        return toAjax(examMasterService.updateExamMaster(examMaster));
    }

    /**
     * 删除答卷题目
     */
    @PreAuthorize("@ss.hasPermi('house_sale:ExamMaster:remove')")
    @Log(title = "答卷题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examids}")
    public AjaxResult remove(@PathVariable Long[] examids)
    {
        Long resaechid= examMasterService.selectReseachIdByExamIds(examids);
        int examcount = examMasterService.selCountId(resaechid);
        int count = researchExamService.updExamCount(examcount-1,resaechid);
        System.out.println("count="+count);
        return toAjax(examMasterService.deleteExamMasterByExamids(examids));
    }
}
